<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>mysql常用操作 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>mysql常用操作</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a>
			</span>
		
	</div>

	

	
		<h1 id="mysql常用操作"><a href="#mysql常用操作" class="headerlink" title="mysql常用操作"></a>mysql常用操作</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有一个图形管理mysql的工具叫phpmyadmin，而如何在命令行下面来管理和操作mysql。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show databases;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/01.png?raw=true" alt=""></figure></p>
<h2 id="查看某个库的表"><a href="#查看某个库的表" class="headerlink" title="查看某个库的表"></a>查看某个库的表</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; use mysql;</div><div class="line">mysql&gt; show tables;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/02.png?raw=true" alt=""></figure></p>
<h2 id="查看表的字段"><a href="#查看表的字段" class="headerlink" title="查看表的字段"></a>查看表的字段</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; desc pre_ucenter_vars;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/03.png?raw=true" alt=""></figure></p>
<h2 id="查看建表的语句"><a href="#查看建表的语句" class="headerlink" title="查看建表的语句"></a>查看建表的语句</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show create table pre_ucenter_vars\G;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/04.png?raw=true" alt=""></figure></p>
<h2 id="当前是哪个用户"><a href="#当前是哪个用户" class="headerlink" title="当前是哪个用户"></a>当前是哪个用户</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; select user();</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/05.png?raw=true" alt=""></figure></p>
<h2 id="查看当前库"><a href="#查看当前库" class="headerlink" title="查看当前库"></a>查看当前库</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; select database();</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/06.png?raw=true" alt=""></figure></p>
<h2 id="创建库"><a href="#创建库" class="headerlink" title="创建库"></a>创建库</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create database yanyi;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/07.png?raw=true" alt=""></figure></p>
<h2 id="创建表"><a href="#创建表" class="headerlink" title="创建表"></a>创建表</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table tb1 (`id` int(4), `name` char(40)) ENGINE=MyISAM DEFAULT CHARSET=gbk;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tb1 是表名；第一个字段是 <code>id</code>,格式是 int ，长度 4位；第二个字段 <code>name</code> 格式是 char 长度 40；指定 ENGINE 为 MyISAM ；字符集 DEFAULT CHARSET 为 gbk。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/08.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建完成后查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/09.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/10.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/11.png?raw=true" alt=""></figure></p>
<h2 id="插入数据"><a href="#插入数据" class="headerlink" title="插入数据"></a>插入数据</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into tb1 values (1,<span class="string">'yanyi'</span>);</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/12.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还可以继续插入</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/13.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以只插入一个字段</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into tb1 (`id`) values (2);</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/14.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into tb1 (`name`) values (<span class="string">'docker'</span>);</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/15.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into tb1 (`name`,`id`) values (<span class="string">'redis'</span>,6);</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/16.png?raw=true" alt=""></figure></p>
<h2 id="查看数据库版本"><a href="#查看数据库版本" class="headerlink" title="查看数据库版本"></a>查看数据库版本</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; select version();</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/17.png?raw=true" alt=""></figure></p>
<h2 id="查看-mysql-状态"><a href="#查看-mysql-状态" class="headerlink" title="查看 mysql 状态"></a>查看 mysql 状态</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show status;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/18.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以用 like 过滤，% 通配</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show status like <span class="string">'%running'</span>;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/19.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show status like <span class="string">'%buffer%'</span>;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/20.png?raw=true" alt=""></figure></p>
<h2 id="修改-mysql-参数"><a href="#修改-mysql-参数" class="headerlink" title="修改 mysql 参数"></a>修改 mysql 参数</h2><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/21.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; <span class="built_in">set</span> global max_connections=200;</div><div class="line">mysql&gt; show variables like <span class="string">'max_connections'</span>;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/22.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果记不住也可以用 % 通配</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show variables like <span class="string">'max_connec%'</span>;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/23.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不过这些方法，重启就会变回以前配置，要永久生效需更改 /etc/my.cnf</p>
<h2 id="查看-mysql-队列"><a href="#查看-mysql-队列" class="headerlink" title="查看 mysql 队列"></a>查看 mysql 队列</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show processlist;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/24.png?raw=true" alt=""></figure></p>
<h2 id="查看-mysql-变量"><a href="#查看-mysql-变量" class="headerlink" title="查看 mysql 变量"></a>查看 mysql 变量</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show variables;</div></pre></td></tr></table></figure>
<h2 id="创建普通用户并授权"><a href="#创建普通用户并授权" class="headerlink" title="创建普通用户并授权"></a>创建普通用户并授权</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">mysql&gt; grant all on *.* to user1 identified by <span class="string">'123456'</span>;</div><div class="line"></div><div class="line">mysql&gt; grant all on discuz.* to <span class="string">'user2'</span>@<span class="string">'192.168.0.%'</span> identified by <span class="string">'123456'</span>；</div><div class="line"></div><div class="line">mysql&gt; grant all on discuz.* to <span class="string">'user3'</span>@<span class="string">'%'</span> identifined by <span class="string">'123456'</span>;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;all 代表所有权限；<em>.</em>代表所有库里的所有表，discuz.*代表 discuz 库里的所有表；to 后边是用户名；@ 后边是客户端 ip ，192.168.0.%代表整个网段，% 代表通配，直接用 % 代替 ip 就表示所有网段；identfied by 后边是密码。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用户创建完以后还没有即时生效，需要刷新下权限命令如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; flush privileges;</div></pre></td></tr></table></figure>
<h2 id="更改密码"><a href="#更改密码" class="headerlink" title="更改密码"></a>更改密码</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; update mysql.user <span class="built_in">set</span> password=password(<span class="string">"newpwd"</span>) <span class="built_in">where</span> user=<span class="string">'username'</span>;</div></pre></td></tr></table></figure>
<h2 id="查询"><a href="#查询" class="headerlink" title="查询"></a>查询</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; select count(*) from mysql.user;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/25.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; select * from mysql.db;select * from mysql.db <span class="built_in">where</span> host like <span class="string">'10.0.%'</span>\G;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/26.png?raw=true" alt=""></figure></p>
<h2 id="插入"><a href="#插入" class="headerlink" title="插入"></a>插入</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; update tb1 <span class="built_in">set</span> id=5 <span class="built_in">where</span> name = <span class="string">'docker'</span>;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/27.png?raw=true" alt=""></figure></p>
<h2 id="清空表"><a href="#清空表" class="headerlink" title="清空表"></a>清空表</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; truncate table yanyi.tb1;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;yanyi是库名，tb1是表名，清空以后表还在。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/28.png?raw=true" alt=""></figure></p>
<h2 id="删除表"><a href="#删除表" class="headerlink" title="删除表"></a>删除表</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; delete from tb1 <span class="built_in">where</span> name=<span class="string">'redis'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是删除表中指定的行</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/29.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; drop table tb1;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是删除整个表</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/30.png?raw=true" alt=""></figure></p>
<h2 id="删除数据库"><a href="#删除数据库" class="headerlink" title="删除数据库"></a>删除数据库</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; drop database yanyi;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/31.png?raw=true" alt=""></figure></p>
<h2 id="修复表"><a href="#修复表" class="headerlink" title="修复表"></a>修复表</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; repair table discuz.pre_forum_post;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/32.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上就是一些常用的 mysql相关操作。还有个知识点，在 shell 的命令下去执行 mysql 的操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># mysql -uroot -pyanyi mysql -e "show tables"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-e 前面的 mysql 指的是库的名字， -e 选项后面双引号括起来的就是 mysql 的命令。 </p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C/33.png?raw=true" alt=""></figure></p>
<h2 id="授权超级用户"><a href="#授权超级用户" class="headerlink" title="授权超级用户"></a>授权超级用户</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">grant all privileges on *.* to <span class="string">'tangnanbing'</span>@<span class="string">'%'</span> identified by <span class="string">'1qaz@WSX'</span> with grant option;</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/MySQL/13. mysql常用操作/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
